<?php
	require_once("conn.php");
	require_once("smarty/Smarty.class.php");

	//about types
    	session_start();
	if(isset($_POST['radiobutton'])){
		$_SESSION['flag'] = $_POST['radiobutton'];
	}
	if(!(isset($_GET['type_id']))){
		$type_id_within_web = 1;
	}
	else{
		$type_id_within_web = $_GET['type_id'];
	}

	$type_id_within_array = $type_id_within_web-1;

	if(!(isset($_GET['article_id']))){
		$article_id_within_web = 1;
	}
	else{
		$article_id_within_web = $_GET['article_id'];
	}
	$article_id_within_array = $article_id_within_web-1;
	$article_id_within_web_prev = $article_id_within_web-1;
	$article_id_within_web_next = $article_id_within_web+1;
	mysql_select_db($dataname,$conn);
	$query_rs_types = "select * from types order by type_id ASC";
	mysql_query("set names 'gbk'");
	$rs_types = mysql_query($query_rs_types,$conn) or die(mysql_error());
	$array_rs_types = array();
	$array_rs_type_name = array();
	$array_rs_type_id = array();

	while($row_rs_types = mysql_fetch_assoc($rs_types)){
		array_push($array_rs_types,$row_rs_types);
		array_push($array_rs_type_name,$row_rs_types['type_name']);
		array_push($array_rs_type_id,$row_rs_types['type_id']);
	}

    //about artilces

    mysql_select_db($dataname,$conn);
    $query_rs_article_one = sprintf("select * from articles where
	article_id=%s",$article_id_within_web);
	$query_rs_article_one_prev = sprintf("select * from articles where
	article_id=%s",$article_id_within_web_prev);
	$query_rs_article_one_next = sprintf("select * from articles where
	article_id=%s",$article_id_within_web_next);

	$query_rs_articles = "select * from articles order by last_update_time DESC";
    $query_rs_articles_limit = "select * from articles limit 0,10 order by last_update_time DESC";
	mysql_query("set names 'gbk'");
    $rs_articles = mysql_query($query_rs_articles,$conn) or die(mysql_error());
    $rs_article_one = mysql_query($query_rs_article_one,$conn) or die(mysql_error());
	$rs_article_one_prev = mysql_query($query_rs_article_one_prev,$conn) or die(mysql_error());
	$rs_article_one_next = mysql_query($query_rs_article_one_next,$conn) or die(mysql_error());
	$rs_articles_num = mysql_num_rows($rs_articles);
	$row_rs_article_one = mysql_fetch_assoc($rs_article_one);
	$row_rs_article_one_prev = mysql_fetch_assoc($rs_article_one_prev);
	$row_rs_article_one_next = mysql_fetch_assoc($rs_article_one_next);

	$array_rs_articles = array();
	$array_rs_article_id = array();
    $array_rs_articles_limit = array();
    $array_rs_articles_random = array();
	$array_rs_articles_random_limit = array();

    while($row_rs_articles = mysql_fetch_assoc($rs_articles)){
		array_push($array_rs_articles,$row_rs_articles);
		array_push($array_rs_article_id,$row_rs_articles['article_id']);
	}

	$array_rs_articles_random = array_slice($array_rs_articles,0);
    shuffle($array_rs_articles_random);
	$array_rs_articles_random_limit = array_slice($array_rs_articles_random,0,10);
	$array_rs_articles_limit = array_slice($array_rs_articles,0,10);


   //about config

	mysql_select_db($dataname,$conn);
	$query_rs_config = "select * from config";
    $rs_config = mysql_query($query_rs_config,$conn) or die(mysql_error());
	$row_rs_config = mysql_fetch_assoc($rs_config);
	$row_rs_config_copyright = $row_rs_config['copyright'];

	//echo $row_rs_article_one['title'];

   //about smarty

	$smarty = new Smarty;
    $smarty->left_delimiter = '[{';
	$smarty->right_delimiter = '}]';

	$smarty->assign("array_rs_types",$array_rs_types);
	$smarty->assign("array_rs_type_name",$array_rs_type_name);
	$smarty->assign("array_rs_type_id",$array_rs_type_id);
	$smarty->assign("array_rs_articles",$array_rs_articles);
	$smarty->assign("array_rs_articles_limit",$array_rs_articles_limit);
	$smarty->assign("array_rs_articles_random_limit",$array_rs_articles_random_limit);
	$smarty->assign("row_rs_config_copyright",$row_rs_config_copyright);
	$smarty->assign("article_id_within_web",$article_id_within_web);
	$smarty->assign("article_id_within_array",$article_id_within_array);
	$smarty->assign("type_id_within_web",$type_id_within_web);
	$smarty->assign("type_id_within_array",$type_id_within_array);
	$smarty->assign("row_rs_article_one",$row_rs_article_one);
	$smarty->assign("row_rs_article_one_prev",$row_rs_article_one_prev);
	$smarty->assign("row_rs_article_one_next",$row_rs_article_one_next);
	$smarty->assign("rs_articles_num",$rs_articles_num);
	$smarty->display('view'.$_SESSION['flag'].'.tpl');

?>